#pragma once
#pragma comment( lib, "strsafe.lib" )

#include <strsafe.h>

#include "HTTPMgr.h"
#include "STLUtil.h"
#include "TimerQueue.h"


static const TCHAR * CAFE_URL_FORMAT			= L"http://cafe.naver.com/ArticleRead.nhn?clubid=%d";
static const TCHAR * ARTICLE_ID_FORMAT			= L"articleid=%d";
static const TCHAR * CAFE_ARTICLE_URL_FORMAT	= L"http://cafe.naver.com/ArticleRead.nhn?clubid=%d&articleid=%d";

static const TCHAR * CONFIG_FILE_NAME	= _T( "NaverCafeKeyWordMonitor.cfg" );

typedef struct _Article
{
	INT		nID;
	tstring	sTitle;

	_Article( VOID ) :
		nID( NULL )
	{
	}

	_Article( INT nID, LPCTSTR pszTitle )
	{
		this->nID	= nID;
		this->sTitle.assign( pszTitle );
	}


} Article;

class CCafeArticleList
{
	public	:

				CCafeArticleList( VOID );
		virtual	~CCafeArticleList( VOID );

		BOOL	Parse( tstring &sContents );

		VOID	CleanUp( VOID );

		deque< Article * > *
				GetArticleList( VOID );

	private	:

		deque< Article * >
				m_dequeArticle;

		BOOL	Parse( tstring & sOri, tstring::size_type & nOffSet, LPCTSTR pszFront, LPCTSTR pszEnd, tstring & sParsed );

		BOOL	Add( INT nID, LPCTSTR pszTitle );
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

class CNaverCafeKeyWordMonitor
{
	public	:
						CNaverCafeKeyWordMonitor( VOID );
		virtual			~CNaverCafeKeyWordMonitor( VOID );

		BOOL			Initialize( INT_PTR nClubID, DWORD dwCheckCycle, TCHAR * pszKeyWords, UINT nKeyWordCNT );

		INT_PTR			GetClubID( VOID );
		INT_PTR			GetPreviosArticleID( VOID );

		BOOL			DoCheck( VOID );

	private	:

		INT_PTR			m_nClubID;
		INT_PTR			m_nPrevArticleID;

		INT_PTR			m_nCheckCycle;

		CHTTPMgr		m_HTTPMgr;
		CCafeArticleList
						m_CafeArticleList;
		CTimerQueue		m_TimerQueue;

		BOOL			GetArticleURL( INT_PTR nArticleID, tstring & sURL );
		BOOL			ReadArticle( INT_PTR nArticleID, tstring & sContents );
};
